package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.service.impl.AdminUserServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/admin/user/*")
public class AdminUserController extends HttpServlet {

    AdminUserService adminUserService = new AdminUserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/user/", "");
        switch (op) {
            case "list":
                list(req, resp);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/user/", "");
        switch (op) {
            case "update":
                update(req, resp);
                break;
        }

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String s = req.getReader().readLine();
        String nickname = JacksonUtil.parseString(s, "nickname");
        String status = JacksonUtil.parseString(s,"status");
        String gender = JacksonUtil.parseString(s, "gender");
        String mobile = JacksonUtil.parseString(s, "mobile");
        String password = JacksonUtil.parseString(s, "password");
        String id = JacksonUtil.parseString(s, "id");
        List<String> updateList = new ArrayList<>();
        updateList.add("nickname"+":"+nickname);
        updateList.add("status"+":"+status);
        updateList.add("gender"+":"+gender);
        updateList.add("mobile"+":"+mobile);
        updateList.add("password"+":"+password);
        adminUserService.update(nickname,status,gender,password,mobile,id);


        Map<String,Integer> map = new HashMap<>();
        map.put("data",1);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(map)));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String username = req.getParameter("username");
        String mobile = req.getParameter("mobile");
        List<MarketUser> userList = adminUserService.list(page, limit, sort, order,username, mobile);
        Object o = ResponseUtil.okList(userList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
